﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace _自用SqlHelper
{
    public class SqlHelper
    {
        private string WebConfigString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();


        public static int ExecuteNonQuery(string ConnString, string sql, CommandType commType, params SqlParameter[] para)
        {
            int Result = -2;
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = commType;
                    cmd.CommandText = sql;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    Result = cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            return Result;
        }

        public static object ExecuteScalar(string ConnString, string sql, CommandType commType, params SqlParameter[] para)
        {
            object Result = new object();
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = sql;
                    cmd.CommandType = commType;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    Result = cmd.ExecuteScalar();
                }
                conn.Open();
            }
            return Result;
        }

        //public static SqlDataReader ExecuteReader(string ConnString, string sql, CommandType commType, params SqlParameter[] para)
        //{
        //    using (SqlConnection conn = new SqlConnection(ConnString))
        //    {
        //        conn.Open();
        //        using (SqlCommand cmd = new SqlCommand())
        //        {
        //            cmd.CommandText = sql;
        //            cmd.CommandType = commType;
        //            cmd.Connection = conn;
        //            cmd.Parameters.AddRange(para);
        //            SqlDataReader sdr = cmd.ExecuteReader();
        //            return sdr;
        //        }
        //        conn.Close();
        //    }
        //}


        public static DataSet GetDataSet(string ConnString, string sql, CommandType commType, params SqlParameter[] para)
        {
            DataSet sd = new DataSet();
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConnString;
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = commType;
                    cmd.Parameters.AddRange(para);
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        sda.SelectCommand = cmd;
                        sda.Fill(sd);
                    }
                }
            }
            return sd;
        }



    }
}
